package com.heima.home.service.impl;

import com.heima.home.entity.ReviewLikes;
import com.heima.home.mapper.ReviewLikesMapper;
import com.heima.home.service.ReviewLikesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
@Service
public class ReviewLikesServiceImpl implements ReviewLikesService {
    @Autowired
    private ReviewLikesMapper reviewLikesMapper;

    @Override
    public int countReviewLikes(Integer reviewId) {
        return reviewLikesMapper.countReviewLikes(reviewId);
    }

    @Override
    public int isLiked(ReviewLikes reviewLikes) {
        return reviewLikesMapper.isLiked(reviewLikes);
    }

    @Override
    public int toggleReviewLike(ReviewLikes reviewLikes) {
        // 检查用户是否已点赞
        int liked = reviewLikesMapper.isLiked(reviewLikes);
        
        if (liked > 0) {
            // 已点赞，执行取消点赞
            reviewLikesMapper.deleteReviewLikes(reviewLikes);
            return 0; // 返回0表示取消点赞
        } else {
            // 未点赞，执行点赞
            reviewLikes.setCreatedAt(LocalDateTime.now());
            reviewLikesMapper.addReviewLikes(reviewLikes);
            return 1; // 返回1表示点赞成功
        }
    }

}
